﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Status
{
    public class Status : IPlugin.IPlugin
    {
        #region InfoPlugin

        public string[] Authors
        {
            get
            {
                return new string[] { "Manuel Fernandez" };
            }
        }

        public string Name
        {
            get
            {
                return "Status";
            }
        }

        public string Description
        {
            get
            {
                return "Shows the current status and global configuration of the proxy";
            }
        }

        public IPlugin.PluginRing Ring
        {
            get
            {
                return IPlugin.PluginRing.Ring0;
            }
        }
        #endregion

        public Model.Elements.HttpPacket Start(Model.Elements.HttpPacket packet)
        {
            if (packet is Model.Elements.HttpRequestPacket)
            {
                Model.Elements.HttpRequestPacket request = packet as Model.Elements.HttpRequestPacket;


                #region /status
                if (request.uri == "/status")
                {
                    
                    // Auth
                    string user = "proxyme";
                    string password = "proxyme";
                    if (!IPlugin.API.General.CheckBasicAuth(request, user, password))
                    {
                        // If not authenticated, 401 Auth is requested
                        Model.Elements.HttpResponsePacket responseAuth = new Model.Elements.HttpResponsePacket(request.version, "401", "Auth");
                        responseAuth.AddHeader(new Model.Elements.HttpHeader("WWW-Authenticate", @"Basic realm=""insert realm"""));
                        request.SetforcedResponsePacket(responseAuth);
                        responseAuth.SetRequestPacket( request);
                        return request;
                    }

                    IPlugin.Session session = IPlugin.Sessions.GetSession(request);
                    
                    StringBuilder html = new StringBuilder();
                    html.Append("<html>");
                    html.Append("<center><h2>Configuration</h2></center><br/>");

                    
                    

                    if (session == null)
                    {
                        html.Append(string.Concat("<br/>Session doesn't exist! :(<br/>"));
                    }
                    else
                    {
                        DictionaryEntry[] dictArr = session.GetObjects();

                        if (dictArr.Length > 0)
                        {
                            html.Append(string.Concat("<br/>Session table (", session.hash, "):<br/>"));
                            html.Append("<center>");
                            html.Append("<table>");
                            html.Append("<tr style='background-color: navy; color: white;'><td>name</td><td>value</td></tr>");
                            foreach (DictionaryEntry dict in dictArr)
                            {
                                html.Append("<tr>");
                                html.Append(string.Format("<td>{0}</td>", dict.Key));
                                html.Append(string.Format("<td>{0}</td>", dict.Value));
                                html.Append("</tr>");
                            }
                            html.Append("</table>");
                            html.Append("</center>");
                        }
                    }
                    
                    

                    html.Append(string.Format("Listen at {0}<br/>", Configuration.Configuration.port));
                    html.Append(string.Format("Open proxy: {0}<br/>", Configuration.Configuration.openProxy));
                    html.Append(string.Format("Debug mode: {0}<br/>", Configuration.Configuration.debug.ToString()));
                    html.Append(string.Format("Log folder: {0}<br/>", Configuration.Configuration.logsPath));

                    html.Append("<br/>Routing table:<br/>");
                    html.Append("<center>");
                    html.Append("<table>");
                    html.Append("<tr style='background-color: navy; color: white;'><td>vhost</td><td>forwarded to</td><td>as</td></tr>");
                    foreach (Configuration.RelationDomainIp relation in Configuration.Configuration.lstRelationDomainIp)
                    {
                        html.Append("<tr>");
                        html.Append(string.Format("<td>{0}</td>", relation.domain));
                        html.Append(string.Format("<td>{0}:{1}</td>", relation.remoteIp, relation.remotePort));
                        html.Append(string.Format("<td>{0}</td>", relation.remoteHostName));
                        html.Append("</tr>");
                    }
                    html.Append("</table>");
                    html.Append("</center>");
                    html.Append("</html>");

                    Model.Elements.HttpResponsePacket response = new Model.Elements.HttpResponsePacket(request.version, "200", "OK");
                    response.AddHeader(new Model.Elements.HttpHeader("Content-Type", "text/html"));
                    response.AddHeader(new Model.Elements.HttpHeader("Content-Length", html.Length.ToString()));
                    response.data = ASCIIEncoding.ASCII.GetBytes(html.ToString());
                    request.SetforcedResponsePacket(response);
                    response.SetRequestPacket( request);
                    return request;
                    
                }
                #endregion
                
            }

            return packet;
        }
    }
}
